/*
 * match_test.cc
 *
 *  Created on: Mar 28, 2012
 *      Author: tqlong
 */

#include <iostream>
#include <boost/program_options.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/foreach.hpp>
#include <armadillo>

#include "stl-helper.h"
#include "set_match.h"

namespace po = boost::program_options;
po::options_description desc("Allowed options");
po::variables_map vm;

void process_options(int argc, char** argv);
void usage(int exitcode = 0);

void read_test();
void kd_build_test();

int main(int argc, char** argv)
{
  std::cerr << "match tests ..." << std::endl;
  srand(time(NULL));

  process_options(argc, argv);

  boost::posix_time::ptime time_start(boost::posix_time::microsec_clock::local_time());

  if (vm.count("help"))
    usage();
  else
  {
    if (vm.count("read"))
      read_test();
    else if (vm.count("kd"))
      kd_build_test();
  }

  boost::posix_time::ptime time_end(boost::posix_time::microsec_clock::local_time());
  boost::posix_time::time_duration duration(time_end - time_start);
  std::cerr << "Duration = " << duration << "\n";
}

void read_test()
{
  std::string inputFile = vm["input"].as<std::string>();
  vvd_t s1;
  
  std::ifstream input;
  FileAccess::open2read(input, inputFile);
  FileAccess::readrows(input, s1);
  input.close();

  vvd_t s2 = s1;
  vi_t perm = RAND::randperm(sz(s1));
  for (int i = 0; i < sz(s1); i++)
    s2[i] = s1[perm[i]];

  BOOST_FOREACH(const vd_t& v, s1) {
    v.print("v1=");
  }
  BOOST_FOREACH(const vd_t& v, s2) {
    v.print("v2=");
  }

  SimpleMatch m;
  m.run(s1, s2);

  std::string outputFile = vm["output"].as<std::string>();
  std::ofstream output;
  FileAccess::open2write(output, outputFile);
  for (int i1 = 0; i1 < sz(m.m12()); i1++) {
    output << i1 << " <--> " << m.m12(i1) << ENDL;
  }
  output.close();
}

void kd_build_test()
{
  std::string inputFile = vm["input"].as<std::string>();
  vvd_t s;
  
  std::ifstream input;
  FileAccess::open2read(input, inputFile);
  FileAccess::readrows(input, s);
  input.close();

  MSG("kd_build n=" << sz(s));

  KDNodeData root_data(s);
  TreeNode<KDNodeData> root(root_data);
}

void process_options(int argc, char** argv)
{
  desc.add_options()
    ("help", "produce this usage")
    ("input", po::value<std::string>(), "input file")
    ("output", po::value<std::string>(), "output file")
    ("annotation", po::value<std::string>(), "annotation file")
    ("freq", po::value<std::string>(), "frequency file")

    // PARAMETERS
    ("pattern", po::value<std::string>(), "pattern")
    ("mtlen", po::value<int>()->default_value(6), "motif length")
    ("iter", po::value<int>()->default_value(1), "maximum number of iteration")
    ("simple", "simple annotation file (ORF only)")

    //JOB TYPE
    ("read", "read genome and annotation")
    ("kd", "kd-tree builder")
    ;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
}

void usage(int exitcode)
{
  std::cout << desc << std::endl;
  exit(exitcode);
}
